/*
 * Created by JFormDesigner on Sun May 23 20:59:38 CST 2021
 */

package com.dfrz.bookstore.view;

import java.awt.event.*;

import com.dfrz.bookstore.entry.*;
import com.dfrz.bookstore.service.OrderItemService;
import com.dfrz.bookstore.service.impl.*;

import java.awt.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.List;
import javax.swing.*;

/**
 * @author Brainrain
 */
public class BookInfo extends JFrame {
    ShoppingCarServiceImpl carService = new ShoppingCarServiceImpl();
    BookServiceImpl bookService = new BookServiceImpl();
    Book book;
    User user;
    Admin admin;
    /**
     * 测试用的mian 方法 请无视
     * @param args
     */
    public static void main(String[] args) {
        BookServiceImpl bookService = new BookServiceImpl();
        UserServiceImpl userService = new UserServiceImpl();
        Book book = new Book();

        AdminServiceImpl adminService = new AdminServiceImpl();
        Admin admin = new Admin();

        book.setId(3);
        Book book1 = bookService.findBookByBook(book).get(0);

        User user = new User();
        user.setId(1);

        User user1 = userService.findUserByUser(user).get(0);
//        BookInfo bookInfo = new BookInfo(book1,user1);
        BookInfo bookInfo = new BookInfo(book1,admin);
        bookInfo.setVisible(true);
    }



    public BookInfo(Book book, Admin admin) {
        initComponents();
        this.admin = admin;
        this.book = book;
        /*管理员的渲染图书信息*/
        showInfo();
    }

    public BookInfo(Book book, User user) {
        initComponents();
        this.book = book;
        this.user = user;
        /*用户的渲染图书信息*/
        showInfo();
    }
    JSpinner number ;
    private JButton insertCar;
    private JButton buy;
    private JButton alterBook;
    private JButton delBook;
    private JButton offShelfBook;
    public void showInfo(){
        //---- number ----
        number = new JSpinner();
        number.setModel(new SpinnerNumberModel(1, 1, book.getStore(), 1));
        panel1.add(number);
        number.setBounds(470, 460, 100, number.getPreferredSize().height);

        panel1.setBackground(new Color(199, 222, 152, 83));
        setTitle(book.getName());
        ImageIcon icon = null;
        try {
            icon = new ImageIcon(new URL(book.getImg_src()));
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        //改变图片大小
        //80和100为大小 可以自由设置
        icon.setImage(icon.getImage().getScaledInstance(335,510,Image.SCALE_DEFAULT));
        img.setIcon(icon);

        bookID.setText(String.valueOf(book.getId()));//隐藏了
        bookName.setText(book.getName());
        author.setText(book.getAuthor());
        price.setText(String.valueOf(book.getPrice()));
        type.setText(String.valueOf(book.getType_id()));
        double price_d = book.getPrice()*book.getDiscount()/10;
        DecimalFormat df = new DecimalFormat("#####.00");
        String format = df.format(price_d);
        price_discount.setText(format);
        discount.setText(String.valueOf(book.getDiscount())+"折");
        if ( book.getDisc()==0 ){
            disc.setText("无光碟") ;
        }else if (book.getDisc()==1){
            disc.setText("有光碟");
        }else {
            disc.setText("属性未找到");
        }
        store.setText(String.valueOf(book.getStore())+"本");
        publisher.setText(book.getPublisher());
        if (book.getCreate_time()!=null){
            create_time.setText(book.getCreate_time().toString());
        }else {
            create_time.setText("0000-00-00");
        }

        if (user!=null){ //给用户添加加入购物车,购买按钮

            //---- 加入购物车按钮 ----
            insertCar = new JButton();
            insertCar.setText("加入购物车");
            insertCar.setFont(insertCar.getFont().deriveFont(insertCar.getFont().getSize() + 10f));
            insertCar.addActionListener(e -> insertCarActionPerformed(e));
            panel1.add(insertCar);
            insertCar.setBounds(550, 550, 160, 60);
            //---- 直接购买 ----
            buy = new JButton();
            buy.setText("购买");
            buy.setFont(buy.getFont().deriveFont(buy.getFont().getSize() + 6f));
            buy.addActionListener(e -> buyActionPerformed(e));
            panel1.add(buy);
            buy.setBounds(730, 550, 170, 60);
        }else {//管理员加入    修改 删除 下架 上架  按钮
            //---- flag ----
            JLabel flag = new JLabel();
            if (book.getFlag()==0){
                 flag.setText("下架状态");
            }else {
                 flag.setText("上架状态");
            }
            panel1.add(flag);
            flag.setBounds(410, 545, 70, 30);

             //---- 修改图书按钮 ----
            alterBook = new JButton();
            alterBook .setText("修改");
            alterBook.setFont(alterBook.getFont().deriveFont(alterBook.getFont().getSize() + 6f));
            alterBook.addActionListener(e -> alterBookActionPerformed(e));
            panel1.add(alterBook);
            alterBook.setBounds(730, 350, 110, 60);

            if (book.getFlag()==1){
                //---- 下架图书按钮 ----
                offShelfBook = new JButton();
                offShelfBook.setText("下架");
                offShelfBook.setFont(offShelfBook.getFont().deriveFont(offShelfBook.getFont().getSize() + 6f));
                offShelfBook.addActionListener(e -> offShelfBookActionPerformed(e));
                panel1.add(offShelfBook);
                offShelfBook.setBounds(730, 450, 110, 60);
            }else {
                //---- 上架图书按钮 ----
                JButton putOnTheShelfBook = new JButton();
                putOnTheShelfBook.setText("上架");
                putOnTheShelfBook.setFont(putOnTheShelfBook.getFont().deriveFont(putOnTheShelfBook.getFont().getSize() + 6f));
                putOnTheShelfBook.addActionListener(e -> putOnTheShelfBookActionPerformed(e));
                panel1.add(putOnTheShelfBook);
                putOnTheShelfBook.setBounds(730, 450, 110, 60);
            }


            //-------删除图书按钮---------
            delBook = new JButton();
            delBook.setText("删除");
            delBook.setFont(delBook.getFont().deriveFont(delBook.getFont().getSize() + 6f));
            delBook.addActionListener(e -> delBookActionPerformed(e));
            panel1.add(delBook);
            delBook.setBounds(730, 550, 110, 60);
        }

    }

    /**
     * 管理员删除图书事件
     * @param e
     */
    private void delBookActionPerformed(ActionEvent e) {
        //确认是否要删除
        int opt = JOptionPane.showConfirmDialog(null,
                "请确认你要删除的书吗?", "确认信息",
                JOptionPane.YES_NO_OPTION);
        if (opt == JOptionPane.YES_OPTION) {

            if (book.getFlag()!=0){
                JOptionPane.showMessageDialog(null,"改书正在上架,无法删除");
                return;
            }
            dispose();
            bookService.delectBookById(book.getId());
            new AdminJFrame(admin,new Book()).setVisible(true);
        }


    }

    /**
     * 管理员上架图书事件
     * @param e
     */
    private void putOnTheShelfBookActionPerformed(ActionEvent e) {
        Book book1 = new Book();
        book1.setId(book.getId());
        book1.setFlag(1);
        bookService.upDataBook(book1);
        dispose();
        Book book = bookService.findBookByBook(book1).get(0);
        new BookInfo(book,admin).setVisible(true);
    }
    /**
     * 管理员下架图书事件
     * @param e
     */

    private void offShelfBookActionPerformed(ActionEvent e) {
        Book book1 = new Book();
        book1.setId(book.getId());
        book1.setFlag(0);
        bookService.upDataBook(book1);
        dispose();
        Book book = bookService.findBookByBook(book1).get(0);
        new BookInfo(book,admin).setVisible(true);
    }

    /**
     * 管理员修改图书信息事件
     * @param e
     */
    private void alterBookActionPerformed(ActionEvent e) {
        addbook addbook = new addbook(book);
        addbook.setVisible(true);
        dispose();
    }


    // 加入购物车事件  用户
    private void insertCarActionPerformed(ActionEvent e) {
        //先判断购物车是否存在此图书
        ShoppingCar shoppingCar = new ShoppingCar();
        shoppingCar.setUserId(user.getId());
        shoppingCar.setBookId(book.getId());
        List<ShoppingCar> carList = carService.findCarByCar(shoppingCar);
        if (carList.size()>0){
            ShoppingCar shoppingCar1 = carList.get(0);
            shoppingCar.setId(shoppingCar1.getId());
            shoppingCar.setBookNum(shoppingCar1.getBookNum()+(int)number.getValue());
            carService.upDataShoppingCar(shoppingCar);
        }else {//不存在 插入新的购物车
            carService.insertShoppingCar(user, book, (int) number.getValue());
            number.setValue(1);
        }
    }
    /**
     *返回按钮
     * @param e
     */
    private void backActionPerformed(ActionEvent e) {
        dispose();
        if (user!=null){
            Book book = new Book();
            book.setFlag(1);
            Client client = new Client(user,book);
            client.setVisible(true);
        }else {
            AdminJFrame adminJFrame = new AdminJFrame(admin,new Book());
            adminJFrame.setVisible(true);
        }

    }

    /**
     * 可以直接购买
     * @param e
     */
    private void buyActionPerformed(ActionEvent e) {
        /*调用支付方式 模拟就行了*/
        Double payPrice = book.getPrice()*book.getDiscount()*(int)number.getValue()/10;
        Pay pay = new Pay(user,book,(int)number.getValue());
        pay.setVisible(true);

    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        panel1 = new JPanel();
        img = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();
        label4 = new JLabel();
        back = new JButton();
        author = new JLabel();
        price = new JLabel();
        price_discount = new JLabel();
        label8 = new JLabel();
        label10 = new JLabel();
        bookName = new JLabel();
        label11 = new JLabel();
        create_time = new JLabel();
        label14 = new JLabel();
        discount = new JLabel();
        label15 = new JLabel();
        store = new JLabel();
        label16 = new JLabel();
        type = new JLabel();
        disc = new JLabel();
        label18 = new JLabel();
        publisher = new JLabel();
        label19 = new JLabel();
        bookID = new JLabel();

        //======== this ========
        setIconImage(new ImageIcon("F:\\\u684c\u9762\\bg.jpg").getImage());
        setMinimumSize(new Dimension(20, 39));
        Container contentPane = getContentPane();
        contentPane.setLayout(new GridLayout());

        //======== panel1 ========
        {
            panel1.setLayout(null);
            panel1.add(img);
            img.setBounds(25, 40, 335, 510);

            //---- label2 ----
            label2.setText("\u4f5c\u8005:");
            label2.setFont(label2.getFont().deriveFont(label2.getFont().getSize() + 10f));
            panel1.add(label2);
            label2.setBounds(410, 100, 70, label2.getPreferredSize().height);

            //---- label3 ----
            label3.setText("\u539f\u4ef7:");
            label3.setFont(label3.getFont().deriveFont(label3.getFont().getSize() + 10f));
            panel1.add(label3);
            label3.setBounds(410, 160, 75, 29);

            //---- label4 ----
            label4.setText("\u73b0\u4ef7:");
            label4.setForeground(Color.red);
            label4.setFont(label4.getFont().deriveFont(label4.getFont().getSize() + 10f));
            panel1.add(label4);
            label4.setBounds(410, 345, 70, 29);

            //---- back ----
            back.setText("\u8fd4\u56de");
            back.addActionListener(e -> backActionPerformed(e));
            panel1.add(back);
            back.setBounds(5, 5, 74, 24);

            //---- author ----
            author.setText("ky");
            author.setFont(author.getFont().deriveFont(author.getFont().getSize() + 10f));
            panel1.add(author);
            author.setBounds(535, 100, 170, author.getPreferredSize().height);

            //---- price ----
            price.setText("99");
            price.setFont(price.getFont().deriveFont(price.getFont().getSize() + 10f));
            panel1.add(price);
            price.setBounds(535, 165, 75, price.getPreferredSize().height);

            //---- price_discount ----
            price_discount.setText("199");
            price_discount.setFont(price_discount.getFont().deriveFont(price_discount.getFont().getSize() + 10f));
            panel1.add(price_discount);
            price_discount.setBounds(535, 345, 75, price_discount.getPreferredSize().height);

            //---- label8 ----
            label8.setText("\u6570\u91cf:");
            label8.setFont(label8.getFont().deriveFont(label8.getFont().getSize() + 10f));
            panel1.add(label8);
            label8.setBounds(410, 450, label8.getPreferredSize().width, 29);

            //---- label10 ----
            label10.setText("\u4e66\u540d:");
            label10.setFont(label10.getFont().deriveFont(label10.getFont().getSize() + 10f));
            panel1.add(label10);
            label10.setBounds(new Rectangle(new Point(410, 35), label10.getPreferredSize()));

            //---- bookName ----
            bookName.setFont(bookName.getFont().deriveFont(bookName.getFont().getSize() + 14f));
            bookName.setText("\u4e66\u540d");
            panel1.add(bookName);
            bookName.setBounds(535, 35, 370, 35);

            //---- label11 ----
            label11.setText("\u4e0a\u67b6\u65e5\u671f:");
            label11.setFont(label11.getFont().deriveFont(label11.getFont().getSize() + 8f));
            panel1.add(label11);
            label11.setBounds(15, 583, 95, 52);

            //---- create_time ----
            create_time.setText("2021\u5e745\u670823\u65e5");
            create_time.setFont(create_time.getFont().deriveFont(create_time.getFont().getSize() + 7f));
            panel1.add(create_time);
            create_time.setBounds(110, 585, 140, 47);

            //---- label14 ----
            label14.setText("\u6298\u6263");
            label14.setFont(label14.getFont().deriveFont(label14.getFont().getSize() + 10f));
            panel1.add(label14);
            label14.setBounds(410, 300, label14.getPreferredSize().width, 29);

            //---- discount ----
            discount.setText("discount");
            discount.setFont(discount.getFont().deriveFont(discount.getFont().getSize() + 10f));
            panel1.add(discount);
            discount.setBounds(new Rectangle(new Point(535, 300), discount.getPreferredSize()));

            //---- label15 ----
            label15.setText("\u5e93\u5b58:");
            label15.setFont(label15.getFont().deriveFont(label15.getFont().getSize() + 10f));
            panel1.add(label15);
            label15.setBounds(410, 505, label15.getPreferredSize().width, 29);

            //---- store ----
            store.setText("store");
            store.setFont(store.getFont().deriveFont(store.getFont().getSize() + 10f));
            panel1.add(store);
            store.setBounds(535, 500, 95, 39);

            //---- label16 ----
            label16.setText("\u56fe\u4e66\u7c7b\u522b");
            label16.setFont(label16.getFont().deriveFont(label16.getFont().getSize() + 10f));
            panel1.add(label16);
            label16.setBounds(410, 228, label16.getPreferredSize().width, 29);

            //---- type ----
            type.setText("text");
            type.setFont(type.getFont().deriveFont(type.getFont().getSize() + 10f));
            panel1.add(type);
            type.setBounds(new Rectangle(new Point(535, 228), type.getPreferredSize()));

            //---- disc ----
            disc.setText("\u65e0\u5149\u789f");
            disc.setFont(disc.getFont().deriveFont(disc.getFont().getSize() + 10f));
            panel1.add(disc);
            disc.setBounds(410, 397, disc.getPreferredSize().width, 29);

            //---- label18 ----
            label18.setText("\u51fa\u7248\u793e:");
            label18.setFont(label18.getFont().deriveFont(label18.getFont().getSize() + 8f));
            panel1.add(label18);
            label18.setBounds(285, 585, 70, 47);

            //---- publisher ----
            publisher.setText("text");
            publisher.setFont(publisher.getFont().deriveFont(publisher.getFont().getSize() + 7f));
            panel1.add(publisher);
            publisher.setBounds(365, 585, 335, 47);

            { // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel1.getComponentCount(); i++) {
                    Rectangle bounds = panel1.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel1.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel1.setMinimumSize(preferredSize);
                panel1.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel1);
        pack();
        setLocationRelativeTo(getOwner());

        //---- label19 ----
        label19.setText("\u56fe\u4e66id");

        //---- bookID ----
        bookID.setText("text");
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JPanel panel1;
    private JLabel img;
    private JLabel label2;
    private JLabel label3;
    private JLabel label4;
    private JButton back;
    private JLabel author;
    private JLabel price;
    private JLabel price_discount;
    private JLabel label8;
    private JLabel label10;
    private JLabel bookName;
    private JLabel label11;
    private JLabel create_time;
    private JLabel label14;
    private JLabel discount;
    private JLabel label15;
    private JLabel store;
    private JLabel label16;
    private JLabel type;
    private JLabel disc;
    private JLabel label18;
    private JLabel publisher;
    private JLabel label19;
    private JLabel bookID;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
